package cn.com.djin.ssm.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import cn.com.djin.ssm.mapper.AddressMapper;
import cn.com.djin.ssm.mapper.BaseMapper;
import cn.com.djin.ssm.mapper.GoodColorMapper;
import cn.com.djin.ssm.mapper.GoodMapper;
import cn.com.djin.ssm.mapper.GoodNumMapper;
import cn.com.djin.ssm.mapper.GoodSizeMapper;
import cn.com.djin.ssm.service.BaseService;

public class BaseServiceImpl<T> implements BaseService<T> {

	// 定义操作结果常量
	// 操作成功
	protected static final String SUCCESS = "success";
	// 操作失败
	protected static final String FAIL = "fail";

	// 定义list集合
	protected List<T> list = new ArrayList<T>();
	// 做Layui分页的map集合
	protected Map<String, Object> map = new HashMap<String, Object>();

	// 依赖BaseMapper代理对象
	@Autowired
	protected BaseMapper<T> baseMapper;
	// 注入GoodMapper
	@Autowired
	protected GoodMapper goodMapper;
	
	@Autowired
	protected GoodSizeMapper goodSizeMapper;
	
	@Autowired
	protected GoodColorMapper goodColorMapper;
	@Autowired
	protected GoodNumMapper goodNumMapper;
	@Autowired
	protected AddressMapper addressMapper;

	/**
	 * 添加
	 * 
	 * @param t
	 *            参数对象
	 * @return 添加结果
	 * @throws Exception
	 */
	@Override
	public String saveT(T t) throws Exception {
		if (baseMapper.insert(t) > 0) {
			return SUCCESS;
		} else {
			return FAIL;
		}

	}

	/**
	 * 修改任意字段
	 * 
	 * @param t
	 *            参数对象
	 * @return 修改结果
	 * @throws Exception
	 */
	@Override
	public String updByPramas(T t) throws Exception {
		if (baseMapper.updateByPramas(t) > 0) {
			return SUCCESS;
		} else {
			return FAIL;
		}

	}

	/**
	 * 查询所有数据信息
	 * 
	 * @return 对象集合
	 * @throws Exception
	 */
	@Override
	public List<T> findAll() throws Exception {
		return baseMapper.queryAll();
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 *            数据id
	 * @return 删除结果
	 * @throws Exception
	 */
	@Override
	public String removeById(String id) throws Exception {
		if (baseMapper.deleteById(id) > 0) {
			return SUCCESS;
		} else {
			return FAIL;
		}
	}

	/**
	 * 根据id批量删除
	 * 
	 * @param ids
	 *            数据id数组
	 * @return 删除结果
	 * @throws Exception
	 */
	@Override
	public String removeBatch(String[] ids) throws Exception {
		if (baseMapper.deleteBatch(ids) > 0) {
			return SUCCESS;
		} else {
			return FAIL;
		}
	}

	/**
	 * 根据有（无）条件分页查询
	 * 
	 * @param t
	 *            参数对象
	 * @param page
	 *            当前页
	 * @param limit
	 *            每一页显示的数据条数
	 * @return Map数据集合
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> findLsitByPramas(T t, Integer page, Integer limit) throws Exception {
		map.put("count", baseMapper.queryTotalByPramas(t));
		list = baseMapper.queryListByPramas(t, (page - 1) * limit, limit);
		map.put("data", list);
		return map;
	}

	/**
	 * 根据id查询单个结果集对象
	 * 
	 * @param id
	 *            对象id
	 * @return 单个结果集对象
	 * @throws Exception
	 */
	@Override
	public T findTById(String id) throws Exception {

		return baseMapper.queryObjectById(id);
	}

	/**
	 * 根据多个条件查询单个数据
	 * 
	 * @param t
	 *            条件对象参数
	 * @return 数据对象
	 * @throws Exception
	 */
	@Override
	public T findTByPramas(T t) throws Exception {

		return baseMapper.queryObjectByPramas(t);
	}

	@Override
	public Long findTotalByPramas(T t) throws Exception {

		return baseMapper.queryTotalByPramas(t);
	}
	/**
	 * 根据条件（无条件）查询多个结果
	 * @param t
	 * @param page
	 * @param limit
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<T> findManyByPramas(T t, Integer page, Integer limit) throws Exception {
		
		return baseMapper.queryListByPramas(t, (page-1)*limit, limit);
	}
	@Override
	public List<T> findManyByPramas(T t) throws Exception {
		
		return baseMapper.queryManyByPramas(t);
	}

	@Override
	public T findObjectByPramas(T t) throws Exception {

		return baseMapper.queryObjectByPramas(t);
	}

	@Override
	public List<T> findPageByPramas(Integer page, Integer limit, T t) throws Exception {
		
		return baseMapper.queryListByPramas(t,(page-1)*limit,limit);
	}



	
}
